﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Xml.Linq;
using System.IO;
using System.Xml;

namespace MyTime
{
    public partial class Settings : Form
    {
        private Core.Entities.TimeTracker _timeTracker;
        public Settings()
        {
            InitializeComponent();
            this._timeTracker = new Core.Entities.TimeTracker(Application.ExecutablePath);
            init();
        }

        public Settings(Core.Entities.TimeTracker _timeTracker)
        {
            InitializeComponent();
            this._timeTracker = _timeTracker;
            init();
        }
        public void init()
        {
            intervalTxtBox.Text = _timeTracker.Interval.ToString();
            myTimeDirectoryPathTxtBox.Text = _timeTracker.FileDirectory;
            autoStartCheckBox.Checked = _timeTracker.AutoStart;
        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            double i = _timeTracker.Interval;
            double.TryParse(intervalTxtBox.Text, out i);
            _timeTracker.Interval = i;
            string path = myTimeDirectoryPathTxtBox.Text;
            if (!string.IsNullOrEmpty(path))
            {
                if (!path.Equals(_timeTracker.FileDirectory))
                    _timeTracker.FileDirectory = path;
            }
            _timeTracker.AutoStart = autoStartCheckBox.Checked;
            Close();
        }

        private void browseBtn_Click(object sender, EventArgs e)
        {
            using (FolderBrowserDialog fd = new FolderBrowserDialog())
            {
                if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    myTimeDirectoryPathTxtBox.Text = fd.SelectedPath;
                }
            }
        }

        private void backupBtn_Click(object sender, EventArgs e)
        {
            string b = Path.Combine(_timeTracker.BackupLocation, "MyTime_Backup_" + DateTime.Now.ToString("yyyy-MM-dd"));
            byte[] content = File.ReadAllBytes(_timeTracker.FullFilePath);
            File.WriteAllBytes(b, content);
            MessageBox.Show("Backup finished");
        }
    }
}
